package com.tyd.cams;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.tyd.cams.Adapter.TzggAdapter;
import com.tyd.cams.entity.SkTzgg;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;

import okhttp3.Call;

public class JiaoWuActivity extends AppCompatActivity {
    ArrayList<SkTzgg> list = new ArrayList<>();
    private ListView SkTzggListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jiao_wu);
        getJiaowuNew();
    }

    /*获取教务动态*/
    private void getJiaowuNew() {
        new Thread() {
            public void run() {
                OkHttpUtils.post()
                        .url("http://syist.cn/jw/jwgg.html")/*http://www.syist.cn/jxdt.asp*/
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                Toast.makeText(JiaoWuActivity.this, "教务公告链接服务器超时", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(String response, int id) {
//                                Log.i("获取的返回信息", response);
                                Document document = Jsoup.parse(response);
//                                body > div.main > div.p-r > div.mainbox
                                Elements context = document.select("body > div.Tbox > div.Tbox_R > div.Nbox > ul > li");
//                                String imgurl = document.select("#picbox > li").select("img").attr("src");
                                for (Element element : context) {
                                    String time = element.select("div.newsdate > div.yars").text();
                                    String title = element.select("div.ggname > a").text().substring(0, 10);
                                    String con = element.select("div.ggname > a").text();
                                    String href = element.select("div.ggname > a").attr("href");
                                    SkTzgg tzgg = new SkTzgg(time, title, con, "http://www.syist.edu.cn/" + href);
                                    list.add(tzgg);
                                }
                                SkTzggListView = findViewById(R.id.JiaowuListView);
                                TzggAdapter tzggAdapter = new TzggAdapter(JiaoWuActivity.this, list);
                                SkTzggListView.setAdapter(tzggAdapter);
                                tzggAdapter.notifyDataSetChanged();
                                Log.i("教务公告*教务动态*信息", list.toString());
                                for (SkTzgg skTzgg : list) {
                                    System.out.println(skTzgg);
                                }

                            }
                        });
            }
        }.start();
    }

}